<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Api\Controller;
use App\Models\Cate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CateController extends Controller
{

  //分类
  public function index(Request $request)
  {
    $cates = Cate::getList($request);
    return $this->success($cates);
  }

  // 树形结构分类
  public function treeList(Request $request)
  {
    $cates = Cate::getAllAsTree($request);
    return $this->success($cates);
  }

  // 分类详情
  public function detail($id)
  {
    $cateInfo = Cate::getInfo($id);
    return $this->success($cateInfo);
  }

  // 新增分类
  public function add(Request $request)
  {
    $data = $request->all();
    $rule = [
      'name'     => 'required',
    ];
    $message = [
      'name.required'       => '分类名称不能为空',
    ];
    $validated = $this->validator($data, $rule, $message);
    if (is_bool($validated)) {
      $parentid = $request->parentid;
      $insertData = [
        'parent_id' => $parentid && count($parentid)>0 ? $parentid[count($parentid)-1]:0,
        'parentid' => $request->parentid && count($request->parentid)>0 ? implode(',', $request->parentid):'', 
        'name' => $request->name ?? '',
        'en_name' => $request->en_name ?? '',
        'img' => $request->img ?? '',
        'status' => $request->status,
        'sort' => $request->sort ?? 0,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      $result = DB::table('cates')->insert($insertData);
      return $this->success();
    } else {
      return $validated;
    }
  }

  // 修改分类
  public function update(Request $request)
  {
    $cateInfo = DB::table('cates')->where('id', $request->id)->first();
    if ($cateInfo && $cateInfo->id) {
      $parentid = $request->parentid;
      $updateData = [
        'parent_id' => $parentid && count($parentid)>0 ? $parentid[count($parentid)-1]:$cateInfo->parent_id,
        'parentid' => $request->parentid && count($request->parentid)>0 ? implode(',', $request->parentid):$cateInfo->parentid, 
        'name' => $request->name ?? $cateInfo->name,
        'en_name' => $request->en_name ?? '',
        'img' => $request->img ?? $cateInfo->img,
        'status' => $request->status ?? $cateInfo->status,
        'sort' => $request->sort ?? $cateInfo->sort,
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      $result = DB::table('cates')->where('id', $request->id)->update($updateData);
      if ($result) {
        return $this->success();
      }
      return $this->failed('修改失败');
    } else {
      return $this->failed('分类不存在');
    }
  }

  // 删除分类
  public function delete($id)
  {
    // 查询是否有子分类
    $cateInfo = DB::table('cates')->where('parent_id', $id)->first();
    if(isset($cateInfo->id) && $cateInfo->id) {
      return $this->failed('请先删除子分类');
    }
    // 查询是否有内容
    $topicInfo = DB::table('topics')->where('cateid', $id)->first();
    if(isset($topicInfo->id) && $topicInfo->id) {
      return $this->failed('请先删除分类下的内容');
    }
    $result = Cate::del($id);
    if ($result) {
      return $this->success();
    }
    return $this->failed('删除失败');
  }
}
